import {
	ajax,
	request,
	defaultBefore
} from "@/utils/ajax.js"
import {payModes,requestPayment,lowerKeys} from "@/api/pay.js"
import { getBaseUrl } from "@/utils/config"
// #ifndef MP-WEIXIN
export const outModes = {
	1:{name:"支付宝",key:1,class:'iconfont icon-alipay text-blue'},
	2:{name:"微信",key:2,class:'iconfont icon-wxpay text-green'},
	
}
// #endif

// #ifdef MP-WEIXIN
export const outModes = {
	1:{name:"支付宝",key:1,class:'iconfont icon-alipay text-blue'},
	2:{name:"微信",key:2,class:'iconfont icon-wxpay text-green'},
	
}
// #endif
export const billTypes = {
		1:'系统',
		2:'转账',
		3:'红包',
		4:'退货',
		5:'退款',
		6:'充值',
		7:'送礼'
	}

export const assets = async()=>{
	const res = await ajax({
		url: '/userWallet/getUserInfo'
	})
	return res;
}

const cashRecord = async (pageNo=1,pageSize=10)=>{
	const res = await ajax({
		url: '/userExtractLog/list',
		data:{
			pageNo,
			pageSize
		}
	})
	return res;
}

const cashOut = async ({cardUserName,cardNum,cardName,money,extractForm,mediaUrl})=>{
	const res = await ajax({
		url: '/userExtractLog/extract',
		data:{
			cardUserName,
			cardNum,
			cardName,
			money,
			extractForm,
			mediaUrl
		}
	})
	return res;
}

const incomeList = async ({type=-1,startDate='',endDate='',pageNo=1,pageSize=10})=>{
	const res = await ajax({
		url: '/userIncomeLog/list',
		data:{
			type,
			startDate,
			endDate,
			pageNo,
			pageSize
		}
	})
	return res;
}

export const recharge = async({userId,money,payplatForm})=>{
	const options = await defaultBefore({
			url:`${getBaseUrl()}/userWallet/recharge`,
			method:'GET',
			data:{
				userId,
				money,
				payplatForm,
				// #ifdef APP-PLUS
				payplatType:1
				// #endif
				// #ifdef H5
				payplatType:2
				// #endif
				// #ifdef MP
				payplatType:3
				// #endif
			}
	})
	console.log(options)
	// #ifdef H5
	console.log(options)
	window.open(options.url)
	return true
	// #endif
	try{
		if(recharge.lock){
			throw new Error('正在请求中')
		}
		recharge.lock=true;
		uni.showLoading({
			mask:true,
			title:'加载中...'
		})
		const {data,statusCode} = await request(options)
		console.log(data,statusCode)
		uni.hideLoading()
		if(statusCode==200){
			let orderInfo = data;
			// #ifdef APP-PLUS
			
			if(payplatForm==2){
				orderInfo = lowerKeys(orderInfo)
			}
			// #endif
			
			return requestPayment({
				// #ifdef MP-WEIXIN
					...data,
				// #endif
				orderInfo,
				provider: payModes[payplatForm].provider,
			});
		}else{
			throw new Error('支付失败')
		}
	}catch(e){
		uni.hideLoading()
		uni.showToast({
			title:e.errMsg||'请求失败,请稍后重试',
			icon:'none'
		})
		throw e;
	}finally{
		recharge.lock=false;
	}
}

export const transition = async ({type,number})=>{
	if(transition.lock){
		throw new Error('正在请求中')
	}
	try{
		transition.lock=true;
		const res = await ajax({
			url:'/canUpdateLog/transition',
			data:{
				transitionType:type,
				number
			}
		})
		return res;
	}catch(e){
		throw e
	}finally{
		transition.lock = false;
	}
}

export default {
	assets,
	incomeList,
	cashRecord,
	recharge,
	cashOut,
	transition
}